﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace LedControl
{
    public class DrawPic
    {
        [DllImport("user32.dll ")]
        private static extern int GetDC(int hwnd);
        private Bitmap scrBmp;
        private Graphics g;
        private int LEDPanelX;
        private int LEDPanelY;

        public DrawPic()
        {
            System.IntPtr p = (IntPtr)GetDC(0);//   '取得屏幕 
            g = Graphics.FromHdc(p);

            scrBmp = new Bitmap(72, 36);

            LEDPanelX = 98;// Properties.Settings.Default.LEDPanelX;
            LEDPanelY = 167;// Properties.Settings.Default.LEDPanelY;

        }
        public void DrawScreen(double[] LEDValueList)
        {
            System.IntPtr p = (IntPtr)GetDC(0);//   '取得屏幕 
            g = Graphics.FromHdc(p);
            Color tempColor;
            int Index;
            for (int i = 0; i < 36; i++)
            {
                for (int j = 0; j < 72; j++)
                {
                    if (j % 4 != 3)
                    {
                        Index = i % 4 * 3 + j % 4;
                    }
                    else
                    {
                        Index = 11;
                    }
                    if (Index < 11)
                    {
                        tempColor = Color.FromArgb(255, (int)LEDValueList[Index], 0, 0);
                    }
                    else
                    {
                        tempColor = Color.FromArgb(255, 0, 0, 0);
                    }
                    scrBmp.SetPixel(j, i, tempColor);
                }
            }
            g.DrawImage(scrBmp, new Point(LEDPanelX, LEDPanelY));
        }
        public void DrawScreen(double[] LEDValueList,int sr,int er,int sc,int ec)
        {
            Color tempColor;
            int Index;
            int r, c;
            for (int i = 0; i < 36; i++)
            {
                r = i/4;
                if ((r < (sr - 1)) || (r > (er - 1)))
                {
                    for (int j = 0; j < 72; j++)
                    {
                        tempColor = Color.FromArgb(255, 0, 0, 0);
                        scrBmp.SetPixel(j, i, tempColor);
                    }
                }
                else
                {
                    for (int j = 0; j < 72; j++)
                    {
                        c = j / 4;
                        if ((c < (sc - 1)) || (c > (ec - 1)))
                        {
                            //for (int k = 0; k < 4; j++)
                            //{
                            //    tempColor = Color.FromArgb(255, 0, 0, 0);
                            //    scrBmp.SetPixel(j + k, i, tempColor);
                            //}
                            tempColor = Color.FromArgb(255, 0, 0, 0);
                            scrBmp.SetPixel(j, i, tempColor);
                            //j = j + 3;
                        }
                        else
                        {
                            if (j % 4 != 3)
                            {
                                Index = i % 4 * 3 + j % 4;
                            }
                            else
                            {
                                Index = 11;
                            }
                            if (Index < 11)
                            {
                                tempColor = Color.FromArgb(255, (int)LEDValueList[Index], 0, 0);
                            }
                            else
                            {
                                tempColor = Color.FromArgb(255, 0, 0, 0);
                            }
                            scrBmp.SetPixel(j, i, tempColor);
                        }                        
                    }
                }                
            }
            g.DrawImage(scrBmp, new Point(LEDPanelX, LEDPanelY));
        }
    }
}
